﻿Imports System
Imports System.Data
Imports System.Data.OleDb
Imports DTO
Imports BUS


Public Class frmQuanLyLichGan
    Dim tbIndex As Integer = 0
    Dim vitriClick As Integer

    Private Sub frmQuanLyLichGan_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        dgvLichLap.DataSource = LichGanLapBus.LayDanhSachXeDuocLap()
        dgvLichGan.DataSource = LichGanBus.LayDanhSachLichGan()
    End Sub

    Public Sub HienThi()
        Try
            vitriClick = dgvLichLap.CurrentRow.Index
            Dim maLichGan As Long
            maLichGan = dgvLichLap.Rows(vitriClick).Cells("MaLichGan").Value

            Dim dt As DataTable = LichGanLapBus.LayLichGanLap(maLichGan)

            ckbThuHai.Checked = dt.Rows(0).Item("ThuHai")
            ckbThuBa.Checked = dt.Rows(0).Item("ThuBa")
            ckbThuTu.Checked = dt.Rows(0).Item("ThuTu")
            ckbThuNam.Checked = dt.Rows(0).Item("ThuNam")
            ckbThuSau.Checked = dt.Rows(0).Item("ThuSau")
            ckbThuBay.Checked = dt.Rows(0).Item("ThuBay")
            ckbChuNhat.Checked = dt.Rows(0).Item("ChuNhat")
        Catch ex As Exception
            ckbThuHai.Checked = False
            ckbThuBa.Checked = False
            ckbThuTu.Checked = False
            ckbThuNam.Checked = False
            ckbThuSau.Checked = False
            ckbThuBay.Checked = False
            ckbChuNhat.Checked = False
            Exit Sub
        End Try
    End Sub
    Private Sub dgvLichLap_MouseClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles dgvLichLap.MouseClick
        HienThi()
    End Sub

    Private Sub btnCapNhatLichLap_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCapNhatLichLap.Click
        Dim lglap As New LichGanLapDto
        Dim flag As Boolean = False
        lglap.MaLichGan = dgvLichLap.Rows(vitriClick).Cells("MaLichGan").Value

        If ckbThuHai.Checked = True Or ckbThuBa.Checked = True Or ckbThuTu.Checked = True _
                                    Or ckbThuNam.Checked = True Or ckbThuSau.Checked = True _
                                    Or ckbThuBay.Checked = True Or ckbChuNhat.Checked = True Then
            flag = True
        End If
        If flag = False Then
            If (MessageBox.Show("Sẽ xóa lịch gán nếu không có lặp!", "THÔNG BÁO", MessageBoxButtons.OKCancel) = Windows.Forms.DialogResult.Cancel) Then
                Exit Sub
            End If

            LichGanLapBus.XoaMotLichGanLap(lglap.MaLichGan)
        End If
        lglap.ThuHai = ckbThuHai.Checked
        lglap.ThuBa = ckbThuBa.Checked
        lglap.ThuTu = ckbThuTu.Checked
        lglap.ThuNam = ckbThuNam.Checked
        lglap.ThuSau = ckbThuSau.Checked
        lglap.ThuBay = ckbThuBay.Checked
        lglap.ChuNhat = ckbChuNhat.Checked
        LichGanLapBus.CapNhatMotLichGanLap(lglap)

        dgvLichLap.DataSource = LichGanLapBus.LayDanhSachXeDuocLap()
    End Sub

    Private Sub dgvLichLap_DataSourceChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dgvLichLap.DataSourceChanged
        HienThi()
    End Sub

    Private Sub btnXoaLichLap_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXoaLichLap.Click
        If (MessageBox.Show("Bạn thật sự muốn xóa!", "THÔNG BÁO", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.No) Then
            Exit Sub
        End If
        Dim MaLichGan As Long = dgvLichLap.Rows(vitriClick).Cells("MaLichGan").Value
        LichGanLapBus.XoaMotLichGanLap(MaLichGan)
        dgvLichLap.DataSource = LichGanLapBus.LayDanhSachXeDuocLap()
        HienThi()
    End Sub

    Private Sub btnXoaLichGan_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXoaLichGan.Click
        Try
            Dim vt As Integer = dgvLichLap.CurrentRow.Index

            Dim MaLichGan As Long = dgvLichGan.Rows(vt).Cells("MaLichGan").Value
            LichGanBus.XoaMotLichGan(MaLichGan)
            dgvLichGan.DataSource = LichGanBus.LayDanhSachLichGan()
            'Xóa trong bảng lặp lặp
            LichGanLapBus.XoaMotLichGanLap(MaLichGan)
            dgvLichLap.DataSource = LichGanLapBus.LayDanhSachXeDuocLap()
            HienThi()
        Catch ex As Exception

        End Try

    End Sub

    Private Sub tctrlHienThi_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tctrlHienThi.SelectedIndexChanged

    End Sub

    Private Sub btnThoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThoat.Click
        Close()
    End Sub

End Class